package com.squirrel.michale;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/2 10:25 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2531 {

    public boolean isItPossible(String word1, String word2) {

        char[] chars1 = word1.toCharArray();
        char[] chars2 = word2.toCharArray();

        int charNumber1 = getCharacterNumber(chars1);
        int charNumber2 = getCharacterNumber(chars2);

        if (charNumber1 == word1.length() && charNumber2 == word2.length() && charNumber2 != charNumber2) {
            return false;
        }

        boolean possible = false;

        for (int i = 0; i < chars1.length; i++) {
            for (int j = 0; j < chars2.length; j++) {
                char[] charsTemp1 = word1.toCharArray();
                char[] charsTemp2 = word2.toCharArray();
                swap(charsTemp1, i, charsTemp2, j);
                if (getCharacterNumber(charsTemp1) == getCharacterNumber(charsTemp2)) {
                    possible = true;
                    break;
                }
            }
        }
        return possible;
    }

    private void swap(char[] chars1, int i, char[] chars2, int j) {
        char temp = chars1[i];
        chars1[i] = chars2[j];
        chars2[j] = temp;
    }

    private int getCharacterNumber(char[] chars) {

        HashSet<Character> characters = new HashSet<>();

        for (int i = 0; i < chars.length; i++) {
            characters.add(chars[i]);
        }
        return characters.size();
    }


    public static void main(String[] args) {
        String word1 = "aa";
        String word2 = "bcd";

        LeetCode2531 leetCode2531 = new LeetCode2531();
//        System.out.println(leetCode2531.isItPossible(word1, word2));
//        System.out.println(leetCode2531.isItPossible2(word1, word2));
//        System.out.println(leetCode2531.isItPossible3(word1, word2));
//
//        String word11 = "abcc";
//        String word12 = "aab";
//
//        System.out.println(leetCode2531.isItPossible3(word11, word12));

//        String word21 = "ac";
//        String word22 = "b";
//
//        System.out.println(leetCode2531.isItPossible3(word21, word22));

        String word31 = "aa";
        String word32 = "bb";

        System.out.println(leetCode2531.isItPossible3(word31, word32));
    }


    public boolean isItPossible2(String word1, String word2) {
        Map<Character, Integer> charTypeFrequ1 = new HashMap<>();
        Map<Character, Integer> charTypeFrequ2 = new HashMap<>();
        for (char c : word1.toCharArray()) {
            charTypeFrequ1.merge(c, 1, Integer::sum);
        }
        for (char c : word2.toCharArray()) {
            charTypeFrequ2.merge(c, 1, Integer::sum);
        }
        for (var e : charTypeFrequ1.entrySet()) {
            for (var f : charTypeFrequ2.entrySet()) {
                char x = e.getKey();
                char y = f.getKey();
                if (x == y) {
                    if (charTypeFrequ1.size() == charTypeFrequ2.size()) {
                        return true;
                    }
                } else {
                    int charTypeFrequNew1 = charTypeFrequ1.size() - (e.getValue() == 1 ? 1 : 0) + (charTypeFrequ1.containsKey(y) ? 0 : 1);
                    int charTypeFrequNew2 = charTypeFrequ2.size() - (f.getValue() == 1 ? 1 : 0) + (charTypeFrequ2.containsKey(x) ? 0 : 1);
                    if (charTypeFrequNew1 == charTypeFrequNew2) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public boolean isItPossible3(String word1, String word2) {
        int[] charTypeFrequ1 = new int[26];
        int[] charTypeFrequ2 = new int[26];

        for (char w1 : word1.toCharArray()) {
            charTypeFrequ1[w1 - 'a']++;
        }
        for (char w2 : word2.toCharArray()) {
            charTypeFrequ2[w2 - 'a']++;
        }
        int totalType1 = 0;
        int totalType2 = 0;
        for (int i = 0; i < 26; i++) {
            totalType1 += charTypeFrequ1[i] == 0 ? 0 : 1;
            totalType2 += charTypeFrequ2[i] == 0 ? 0 : 1;
        }
        for (int i = 0; i < charTypeFrequ1.length; i++) {
            if (charTypeFrequ1[i] == 0) {
                continue;
            }
            for (int j = 0; j < charTypeFrequ2.length; j++) {
                if (charTypeFrequ2[j] == 0) {
                    continue;
                }
                if (i == j) {
                    if (totalType1 == totalType2) {
                        return true;
                    }
                } else {
                    int totalTypeNew1 = totalType1 - (charTypeFrequ1[i] == 1 ? 1 : 0) + (charTypeFrequ1[j] == 0 ? 1 : 0);
                    int totalTypeNew2 = totalType2 - (charTypeFrequ2[j] == 1 ? 1 : 0) + (charTypeFrequ2[i] == 0 ? 1 : 0);
                    if (totalTypeNew1 == totalTypeNew2) {
                        return true;
                    }
                }
            }
        }
        return false;

        // 时间复杂度 word1.length + word2.length + 26*2 + 26*26
        // 空间复杂度 26*2
    }

//    作者：灵茶山艾府
//    链接：https://leetcode.cn/problems/make-number-of-distinct-characters-equal/solutions/2050968/mei-ju-jian-ji-xie-fa-by-endlesscheng-tjpp/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
